<?php
/**
 * Login_Data_Forgotten
 * forgotten password functionality
 * @author Adrian Stolarski
 */
class Login_Data_Forgotten implements Login_Data_Mainable, Login_Data_Messagable, 
        Login_Data_Sendable, Login_Data_Preparable {
    
    /**
     *
     * @var type $database
     * database handler
     */
    public $database;
    /**
     *
     * @var type $database_prepare 
     * database prepare handler
     */
    public $database_prepare;
    /**
     *
     * @var type $random_password
     * random password handler
     */
    public $random_password;
    /**
     *
     * @var type $redirect_action
     * current redirect action
     */
    public $redirect_action;
    /**
     *
     * @var type $mail_sender
     * forgotten mail sender
     */
    public $mail_sender;
    
    /**
     * constructor()
     */
    public function __construct() {
        $this->database = new Core_Driver_MysqlImproved;
        $this->random_password = new Core_Controller_Randompassword;
    }
   
    public function main(array $variables = null) {
        //var_dump($variables);
        //die();
        $this->database->connect();
        $variables['user_password'] = $this->random_password->main($variables);
        //echo $variables['user_password'];
        //$variable = $variables;
        //foreach($variables as $key => $value){
            //var_dump($variables);
            //die();
            //$variables[$key] = $this->databasse->escape($value);
        //}
        //var_dump($variables);
        //die();
        $this->prepare_query($variables);
        //echo $this->database_prepare;
        //die();
        $this->database->prepare($this->database_prepare);
        $this->database->query();
        $this->database->dataOperations();
        //echo $this->database_prepare;
        //die();
        $this->send_email($variables);
        $this->message($variables['user_email']);
    }
    
    /**
     * message()
     * lost password message
     * @param type $value
     */
    public function message($value) {
        echo "<script>alert("
        . FORGOTEN_MESSAGE
                .$value.
                ")</script>";
        $this->redirect_action = new Login_Data_Loginredirect;
        $this->redirect_action->main(null);
    }
    
    /**
     * prepare_query()
     * prepare update user password
     * @param array $variables
     */
    public function prepare_query(array $variables = null) {
        //var_dump($variables);
        //die();
        $this->database_prepare = "UPDATE "
                . "users_data"
                . " SET "
                . "user_password='"
                .md5($variables['user_password']).
                "' WHERE "
                . "users_data.user_email='"
                .$variables['user_email'].
                "';";
    }
    /**
     * send_email()
     * send lost password email
     * @param array $variables
     */
    public function send_email(array $variables = null) {
        //var_dump($variables);
        //die();
        $this->mail_sender = new Login_Data_Forgottenmailsender;
        $this->mail_sender->main($variables);
    }

}
